package com.yager.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(urlPatterns = {"/house/to_addHouse","/house/to_myHouses","/order/getUserOrders","/collection/toShowCollection",
"/user/toUserInfo","/order/getHouseOrder","/collection/addHouseCollection","/order/toAddOrder","/accuse/toAddFromHouse"})
public class LoginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("****************请求拦截成功******************");
        HttpServletRequest req=(HttpServletRequest)request;
        HttpServletResponse resp=(HttpServletResponse)response;

        HttpSession session=req.getSession(false);

        if(session==null||session.getAttribute("user")==null){
            resp.setHeader("RED","RED");
            resp.setStatus(HttpServletResponse.SC_FORBIDDEN);
            /*resp.sendRedirect("loginPage.html");*/
        }
        else {
            chain.doFilter(request, response);
        }

    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("------------loginFilter过滤器初始化成功----------");
    }
}
